package LeetCode;

import java.math.BigInteger;

public class LC_043_MultiplyStrings {
    public static void main(String[] args) {

    }

    public String multiply(String num1, String num2) {
        return new BigInteger(num1).multiply(new BigInteger(num2)).toString();
    }

    public String multiply_2(String num1, String num2) {
        num1 = new StringBuilder(num1).reverse().toString();
        num2 = new StringBuilder(num2).reverse().toString();

        int[] b = new int[num1.length() + num2.length()];
        for (int i = 0; i < num1.length(); i++)
            for (int j = 0; j < num2.length(); j++)
                b[i + j] += (num1.charAt(i) - '0') * (num2.charAt(j) - '0');

        for (int i = 0; i < num1.length() + num2.length(); i++) {
            if (b[i] > 9) {
                b[i + 1] += b[i] / 10;
                b[i] %= 10;
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = num1.length() + num2.length() - 1; i >= 0; i--)
            stringBuilder.append(b[i]);
        while (stringBuilder.charAt(0) == '0' && stringBuilder.length() > 1)
            stringBuilder.deleteCharAt(0);

        return stringBuilder.toString();
    }
}
